package com.sctvcloud.bazhong.base;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sctvcloud.bazhong.R;


/**
 * 带标题栏的FragmentActivity基类
 * Created by pc on 2017/4/12.
 */

public class BaseTitleFragmentActivity extends FragmentActivity {

    private RelativeLayout mLayoutTitleBar;
    private RelativeLayout mLayoutLeft;//button_backward_layout;
    private RelativeLayout mLayoutRight;//button_forward_layout;
    private LinearLayout mLayoutRight2;//right_button_b_textlayout;
    private TextView mTitleTextView;
    private TextView mLeftBackwardbButton;
    private TextView mRightForwardButton;
    private ImageButton mRightImgButton;
    private ImageButton mRightImgButton2;
    private TextView mRightText;
    private FrameLayout mContentLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void initViews() {
        super.setContentView(R.layout.activity_base_title);
        mLayoutTitleBar = (RelativeLayout) findViewById(R.id.layout_titlebar);
        mLayoutLeft = (RelativeLayout) findViewById(R.id.button_backward_layout);
        mLayoutRight = (RelativeLayout) findViewById(R.id.button_forward_layout);
        mLayoutRight2 = (LinearLayout) findViewById(R.id.right_button_b_textlayout);
        mTitleTextView = (TextView) findViewById(R.id.text_title);
        mContentLayout = (FrameLayout) findViewById(R.id.layout_content);
        mLeftBackwardbButton = (TextView) findViewById(R.id.button_backward);
        mRightForwardButton = (TextView) findViewById(R.id.button_forward_text);
        mRightImgButton = (ImageButton) findViewById(R.id.right_button);
        //*********右边第二种按钮布局**********
        mRightImgButton2 = (ImageButton) findViewById(R.id.right_button_topimg);
        mRightText = (TextView) findViewById(R.id.button_forward_bottom_text);
        //*************************************
        mLayoutLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onLeftBtnClick(v);
            }
        });
        mLayoutRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRightBtnClick(v);
            }
        });
        mLayoutRight2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRightBtnClick(v);
            }
        });
    }

    /**
     * 设置标题栏背景
     *
     * @param color 颜色
     */
    public void setTitleBackgroundColor(int color) {
        mLayoutTitleBar.setBackgroundColor(color);
    }

    /**
     * 设置标题栏背景
     *
     * @param id 图片
     */
    public void setBackgroundResource(int id) {
        mLayoutTitleBar.setBackgroundResource(id);
    }

    /**
     * 是否显示返回按钮
     *
     * @param leftResid 文字
     * @param show      true则显示
     */
    public void isShowLeftView(int leftResid, boolean show) {
        if (mLeftBackwardbButton != null) {
            if (show) {
                mLeftBackwardbButton.setText(leftResid);
                mLeftBackwardbButton.setVisibility(View.VISIBLE);
            } else {
                mLeftBackwardbButton.setVisibility(View.INVISIBLE);
            }
        } // else ignored
    }

    /**
     * 是否显示返回按钮
     *
     * @param show true则显示
     */
    public void isShowLeftView(boolean show) {
        if (mLeftBackwardbButton != null) {
            if (show) {
                mLeftBackwardbButton.setVisibility(View.VISIBLE);
            } else {
                mLeftBackwardbButton.setVisibility(View.INVISIBLE);
            }
        } // else ignored
    }

    /**
     * 是否显示返回按钮
     *
     * @param rightTxt 文字
     * @param show     true则显示
     */
    public void isShowRightTextView(String rightTxt, boolean show) {
        if (mRightForwardButton != null && mRightImgButton != null) {
            if (show) {
                mRightForwardButton.setText(rightTxt);
                mRightForwardButton.setVisibility(View.VISIBLE);
                mRightImgButton.setVisibility(View.INVISIBLE);
            } else {
                mRightForwardButton.setVisibility(View.INVISIBLE);
            }
        } // else ignored
    }

    /**
     * 是否显示返回按钮
     *
     * @param img  图片
     * @param show true则显示
     */
    public void isShowRightButtonView(int img, boolean show) {
        if (mRightForwardButton != null && mRightImgButton != null) {
            if (show) {
                mRightImgButton.setBackgroundResource(img);
                mRightImgButton.setVisibility(View.VISIBLE);
                mRightForwardButton.setVisibility(View.INVISIBLE);
            } else {
                mRightImgButton.setVisibility(View.INVISIBLE);
            }
        } // else ignored
    }

    /**
     * 右边按钮  上下布局（上图下字）
     *
     * @param img
     * @param text
     * @param show
     */
    public void isShowRightButtonViewTwo(int img, String text, boolean show) {
        if (mLayoutRight != null && mRightImgButton2 != null&&mLayoutRight2 != null && mRightText != null) {
            if (show) {
                mLayoutRight.setVisibility(View.GONE);
                mLayoutRight2.setVisibility(View.VISIBLE);
                mRightImgButton2.setBackgroundResource(img);
                mRightText.setText(text);
            } else {
                mLayoutRight2.setVisibility(View.GONE);
            }
        } // else ignored
    }

    /**
     * 返回按钮点击后触发
     *
     * @param backwardView
     */
    protected void onLeftBtnClick(View backwardView) {
//        Toast.makeText(this, "点击返回，可在此处调用finish()", Toast.LENGTH_LONG).show();
        finish();
    }

    /**
     * 提交按钮点击后触发
     *
     * @param forwardView
     */
    protected void onRightBtnClick(View forwardView) {
//        Toast.makeText(this, "点击提交", Toast.LENGTH_LONG).show();
    }

    //设置标题内容
    @Override
    public void setTitle(int titleId) {
        mTitleTextView.setText(titleId);
    }

    //设置标题内容
    @Override
    public void setTitle(CharSequence title) {
        mTitleTextView.setText(title);
    }

    //设置标题文字颜色
    @Override
    public void setTitleColor(int textColor) {
        mTitleTextView.setTextColor(textColor);
    }


    //取出FrameLayout并调用父类removeAllViews()方法
    @Override
    public void setContentView(int layoutResID) {
        mContentLayout.removeAllViews();
        View.inflate(this, layoutResID, mContentLayout);
        onContentChanged();
    }

    @Override
    public void setContentView(View view) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view);
        onContentChanged();
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view, params);
        onContentChanged();
    }
}
